package SlidingWindow;

/**
 * @ClassName LC134
 * @Description 加油站
 * @Author ZJX
 * @Date 2024/9/20 15:26
 * @Version 1.0
 */
public class LC134 {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        int index = 0;
        int total = 0;
        int cur = 0;
        for (int i = 0; i < gas.length; i++) {
            int net = gas[i] - cost[i];
            cur += net;
            total += net;
            if (cur < 0){
                index = i + 1;
                total = 0;
            }
        }
        return total >= 0 ? index : -1;
    }
}
